package Application.Tool;

import Application.AppJoint;
import Application.Controller.EraserSettingsController;
import Application.Layer.Layer;
import Application.Layer.LayerDelegate;
import Application.Mask.MaskManager;
import javafx.scene.Parent;
import javafx.fxml.FXMLLoader;
import Application.LayerImage.LayerImage;
import javafx.scene.input.MouseEvent;

public class Eraser extends Tool {
    private Parent root;
    private static Eraser ins = new Eraser();

    public void setSize(int size) {
        this.size = size;
    }

    public void setMask(LayerImage mask) {
        this.mask = mask;
    }

    public void setOpacity(int opacity) {
        this.opacity = opacity;
    }

    // 大小
    private int size = 32;
    // 蒙版
    private LayerImage mask;
    // 不透明度
    private int opacity = 255;

    private Eraser() {
        super();

        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/EraserSettings.fxml"));
            root = loader.load();

            AppJoint.getInstance().loadMasksToComboBox(
                    ((EraserSettingsController)loader.getController()).getCombo()
            );
        } catch(Exception e) {
            e.printStackTrace();
        }

        mask = MaskManager.getInstance().getMaskByName("test");
    }

    private int blend(int c1, int c2) {
        int a1 = (c1 >> 24) & 0xff;
        int a2 = (c2 >> 24) & 0xff;
        int r2 = (c2 & 0x00ff0000) >> 16;
        int g2 = (c2 & 0x0000ff00) >> 8;
        int b2 = (c2 & 0x000000ff);

        int a = a2 - a1;
        if(a < 0) {
            a = 0;
        }

        return (a << 24) | (r2 << 16) | (g2 << 8) | b2;
    }

    public void setMask(String maskName) {
        this.mask = MaskManager.getInstance().getMaskByName(maskName);
    }

    @Override public Parent getToolSettings() {
        return root;
    }

    @Override public void onMouseDown(MouseEvent e) {
        onMouseMove(e);
    }

    @Override public void onMouseMove(MouseEvent e) {
        // 点击在imageview中的位置在实际图片中的像素位置
        double pixelx =
                e.getX() / AppJoint.getInstance().getImageView().getFitWidth() * AppJoint.getInstance().getCanvasWidth();
        double pixely =
                e.getY() / AppJoint.getInstance().getImageView().getFitHeight() * AppJoint.getInstance().getCanvasHeight();

        Layer activeLayer = LayerDelegate.getInstance().getLayerById(AppJoint.getInstance().getActiveLayerId());
        for(int i = -size / 2 + 1; i < size / 2 - 1; i++) {
            for(int j = -size / 2 + 1; j < size / 2 - 1; j++) {
                // 实际像素位置
                int imgx = (int)pixelx + i;
                int imgy = (int)pixely + j;
                if(imgx >= 0 && imgx < AppJoint.getInstance().getCanvasWidth() && imgy >= 0 && imgy < AppJoint.getInstance().getCanvasHeight()) {
                    // 蒙版像素位置
                    int maskx = (i + size / 2) * MaskManager.MASK_SIZE / size;
                    int masky = (j + size / 2) * MaskManager.MASK_SIZE / size;
                    // 蒙版灰度
                    int gray = (mask.getPixelArgb(maskx, masky) & 255);
                    int alpha = gray * this.opacity / 255;
                    int oldColor = activeLayer.getArgb(imgx, imgy);
                    int srcColor = (alpha << 24);

                    int finalColor = this.blend(srcColor, oldColor);

                    activeLayer.setArgb(imgx, imgy, finalColor);
                    LayerDelegate.getInstance().refreshSingle(imgx, imgy);
                }
            }
        }
    }

    @Override public void onMouseUp(MouseEvent e) {

    }

    public static Eraser getInstance() {
        return ins;
    }
}
